package openase.audiosearch.comm.request.encoder;

import java.nio.ByteBuffer;

import openase.audiosearch.comm.StringFormatUtils;
import openase.audiosearch.comm.request.AudioRegistration;
import openase.audiosearch.comm.request.RequestType;


public class AudioRegistrationRequestEncoder implements RequestEncoder<AudioRegistration> {

	public ByteBuffer encode(AudioRegistration request) {
		byte[] name = StringFormatUtils.getUtf8(request.getName());
		byte[] audio = request.getAudioInput();

		ByteBuffer buffer = ByteBuffer.allocate(1 + 4 + name.length + 4 + audio.length);
		buffer.put(RequestType.REGISTER.getId());
		buffer.putInt(name.length);
		buffer.put(name);
		buffer.putInt(audio.length);
		buffer.put(audio);

		return buffer;
	}
}
